package com.hice.demo.sbdbra.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.beetl.sql.core.ClasspathLoader;
import org.beetl.sql.core.Interceptor;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.UnderlinedNameConversion;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.ext.DebugInterceptor;
import org.beetl.sql.ext.spring4.BeetlSqlDataSource;
import org.beetl.sql.ext.spring4.BeetlSqlScannerConfigurer;
import org.beetl.sql.ext.spring4.SqlManagerFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;

/**
 * beetlsql配置
 *
 * @author hyson
 * @create 2017-02-20 19:19
 */
@Configuration
public class BeetlsqlConfig {

    /**
     * 配置数据源连接
     *
     * @param env
     * @return
     */
    @Bean(name = "datasource")
    public DataSource druidDataSource(Environment env) {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(env.getProperty("frs.driver"));
        druidDataSource.setUrl(env.getProperty("frs.dbUrl"));
        druidDataSource.setUsername(env.getProperty("frs.dbUserName"));
        druidDataSource.setPassword(env.getProperty("frs.dbPassowrd"));
        druidDataSource.setValidationQuery("SELECT 1 FROM DUAL");
        druidDataSource.setInitialSize(5);
        druidDataSource.setMaxActive(10);
        return druidDataSource;
    }

    /**
     * beetlSQL扫描器配置
     *
     * @param fb
     * @return
     */
    @Bean(name = "beetlSqlScannerConfigurer")
    public BeetlSqlScannerConfigurer getBeetlSqlScannerConfigurer(
            @Qualifier("sqlManagerFactoryBean") SqlManagerFactoryBean fb,
            Environment env) {
        BeetlSqlScannerConfigurer conf = new BeetlSqlScannerConfigurer();
        conf.setBasePackage(env.getProperty("frs.scanPackage"));
        conf.setDaoSuffix("Dao");
        conf.setSqlManagerFactoryBeanName("sqlManagerFactoryBean");

        SQLManager sql;
        try {
            sql = (SQLManager) fb.getObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return conf;
    }

    /**
     * ORM连接管理配置
     *
     * @param datasource
     * @return
     */
    @Bean(name = "sqlManagerFactoryBean")
    @Primary
    public SqlManagerFactoryBean getSqlManagerFactoryBean(@Qualifier("datasource") DataSource datasource) {
        SqlManagerFactoryBean factory = new SqlManagerFactoryBean();
        BeetlSqlDataSource source = new BeetlSqlDataSource();
        source.setMasterSource(datasource);
        factory.setCs(source);
        factory.setDbStyle(new MySqlStyle());
        factory.setInterceptors(new Interceptor[]{new DebugInterceptor()});
        factory.setNc(new UnderlinedNameConversion());
        factory.setSqlLoader(new ClasspathLoader("/sql"));
        return factory;
    }

}
